package top100;



import java.util.regex.Pattern;

/**
 * @author Godc
 * @description
 */
public class addTwoNumbers {
    public static void main(String[] args) {
        ListNode head1 = new ListNode(9);
        ListNode node1 = new ListNode(9);
        ListNode node2 = new ListNode(9);
        ListNode node3 = new ListNode(9);
        ListNode node4 = new ListNode(9);
        ListNode node5 = new ListNode(9);
        ListNode node6 = new ListNode(9);
        head1.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        ListNode head2 = new ListNode(9);
        ListNode node7 = new ListNode(9);
        ListNode node8 = new ListNode(9);
        ListNode node9 = new ListNode(9);
        head2.next = node7;
        node7.next = node8;
        node8.next = node9;
        AddTwoNumbersSolution solution = new AddTwoNumbersSolution();
        solution.addTwoNumbers(head1,head2);
    }
}
class AddTwoNumbersSolution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode temp = head;
        int carry = 0;
        while(l1!=null||l2!=null||carry!=0){
                int v1 = l1==null?0:l1.val;
                int v2 = l2==null?0:l2.val;
                int sum = v1 + v2 + carry;
                ListNode node = new ListNode((sum)%10);
                carry = (sum)/10;
                head.next = node;
                head = node;
                if(l1!=null)l1 = l1.next;
                if(l2!=null)l2 = l2.next;
        }
        // 巧妙放入到while判断循环中
//        if(carry!=0){
//            head.next = new ListNode(1);
//        }
        return temp.next;
    }
}
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}